package com.weita.lightfood.service;


import com.weita.lightfood.model.dao.HealthReportDao;
import com.weita.lightfood.model.entity.HealthReport;
import com.weita.lightfood.model.entity.User;
import com.weita.lightfood.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author IT_Monster at 2019/04/19 21:51.
 * 健康报表service
 */
@Service
public class HealthReportService {
    @Autowired
    private HealthReportDao healthReportDao;
    @Autowired
    private UserService userService;


    /**
     * 生成用户健康表
     *
     * @param userId
     * @return
     */
    public HealthReport makeReport(int userId) throws Exception {
        User user = userService.getByID(userId);
        //健康指数
        Integer index = 85;
        //健康等级
        String level = "A";
        //BMI    体重/身高的平方
        Double bmi = 0.0;
        //BFR
        Double bfr = 0.0;
        //预算热量
        double calorieBudget = 0;
        //身材类型
        String bodyType = "标准";
        double standardWeight = 0;
        standardWeight = (user.getHeight() - 1) * 90;
        //计算用户年龄
        int age = DateUtil.getAge(user.getDateBirth());
        //计算bmi
        bmi = (user.getFirstWeight() / (user.getHeight() * user.getHeight()));
        //计算bfr
        bfr = 1.2 * bmi + 0.23 * age - 5.4 - 10.8 * user.getSex();
        //计算预算热量
        calorieBudget = 655.096 + 9.563 * user.getFirstWeight() + 1.85 * user.getHeight() * 100 - 4.676 * age + 400;

        //如果用户的目标是减肥，则预算热量需要减去相应的卡路里
        if (user.getAim() == 2){
            double lostWeight = user.getFirstWeight() - user.getAimWeight();
            //每天需要减少的体重
            double lostWeightDay = lostWeight / user.getWeightTime();
            //每天减少的热量
            double lostCalorie = lostWeightDay * 7700;
            calorieBudget -= lostCalorie;
        }

        System.out.println(age + " " + user.getFirstWeight() + " " + user.getHeight());
        //计算健康指数
        //bmi类别
        String bmiType = "";
        //bfr类别
        String bfrType = "";
        //bmi+bfr
        String totalType = "";
        //计算bmi类别
        if (bmi >= 18.5 && bmi <= 19.875) {
            bmiType = "A";
        } else if (bmi > 19.875 && bmi <= 21.25) {
            bmiType = "B";
        } else if (bmi > 21.25 && bmi <= 22.625) {
            bmiType = "C";
        } else if (bmi > 22.625 && bmi <= 24) {
            bmiType = "D";
        } else if (bmi > 24 && bmi <= 28) {
            bmiType = "E";
        } else if (bmi > 28) {
            bmiType = "F";
        }
        //计算bfr类别
        if (bfr >= 26 && bfr <= 27.25) {
            bfrType = "1";
        } else if (bfr > 27.25 && bfr <= 28.5) {
            bfrType = "2";
        } else if (bfr > 28.25 && bfr <= 29.75) {
            bfrType = "3";
        } else if (bfr > 29.75 && bfr < 31) {
            bfrType = "4";
        } else if (bfr > 31 && bfr < 36) {
            bfrType = "5";
        } else if (bfr > 36) {
            bfrType = "6";
        }
        totalType = bmiType + bfrType;
        if (totalType.equals("A1") || totalType.equals("A4") || totalType.equals("D1") || totalType.equals("D4")) {
            index = 96;
        } else if (totalType.equals("A2") || totalType.equals("A3") || totalType.equals("D2") || totalType.equals("D3") ||
                totalType.equals("B1") || totalType.equals("B4") || totalType.equals("C1") || totalType.equals("C4")) {
            index = 97;
        } else if (totalType.equals("B2") || totalType.equals("B3") || totalType.equals("C2") || totalType.equals("C3")
        ) {
            index = 98;
        }
        if ((bmi >= 18.5 && bmi <= 24) && (bfr >= 31 && bfr <= 36)) {
            index = 91;
        } else if ((bmi >= 24 && bmi <= 28) && (bfr >= 26 && bfr <= 31)) {
            index = 91;
        } else if ((bmi >= 18.5 && bmi <= 24) && (bfr >= 21 && bfr <= 26)) {
            index = 91;
        } else if ((bmi >= 24 && bmi <= 28) && (bfr >= 31 && bfr <= 36)) {
            index = 82;
        } else if ((bmi < 18.5) && (bfr >= 21 && bfr <= 26)) {
            index = 82;
        } else if ((bmi >= 18.5 && bmi <= 24) && (bfr < 21)) {
            index = 82;
        } else if ((bmi <= 18.5) && (bfr <= 21)) {
            index = 68;
        } else if ((bmi >= 28) && (bfr >= 36)) {
            index = 59;
        }

        //计算健康评级
        if (index >= 96 && index <= 100) {
            level = "S";
        } else if (index >= 86 && index <= 95) {
            level = "A";
        } else if (index >= 76 && index <= 85) {
            level = "B";
        } else if (index >= 61 && index <= 75) {
            level = "C";
        } else if (index >= 0 && index <= 60) {
            level = "F";
        }

        //计算身材类型
        if (bmi < 18.5) {
            bmiType = "A";
        } else if (bmi >= 18.5 && bmi < 24) {
            bmiType = "B";
        } else if (bmi >= 24 && bmi < 28) {
            bmiType = "C";
        } else if (bmi >= 28) {
            bmiType = "D";
        }

        if (bfr < 21) {
            bfrType = "1";
        } else if (bfr >= 21 && bfr < 26) {
            bfrType = "2";
        } else if (bfr >= 26 && bfr < 31) {
            bfrType = "3";
        } else if (bfr >= 31 && bfr < 36) {
            bfrType = "4";
        } else if (bfr > 36) {
            bfrType = "5";
        }

        totalType = bmiType + bfrType;

        if (totalType.equals("A1")) {
            bodyType = "纤细";
        } else if (totalType.equals("A2")) {
            bodyType = "瘦";
        } else if (totalType.equals("B1")) {
            bodyType = "瘦";
        } else if (totalType.equals("B2")) {
            bodyType = "偏瘦";
        } else if (totalType.equals("B3")) {
            bodyType = "标准";
        } else if (totalType.equals("B4")) {
            bodyType = "微胖";
        } else if (totalType.equals("C3")) {
            bodyType = "微胖";
        } else if (totalType.equals("C4")) {
            bodyType = "超重";
        } else if (totalType.equals("D5")) {
            bodyType = "肥胖";
        }

        return new HealthReport(userId, index, level, bmi, bfr, calorieBudget, bodyType, standardWeight);
    }

    /**
     * 生成健康报表
     * @param userId
     * @throws Exception
     */
    public void creatReport(int userId) throws Exception {
        HealthReport healthReport = this.makeReport(userId);
        healthReportDao.creatReport(healthReport);
    }

    /**
     * 获取用户健康报表Integer userId, Integer index, String level,
     * @param userId
     * @return
     * @throws Exception
     */
    public HealthReport getHealthReport(int userId) throws Exception {
        return healthReportDao.getHealthReport(userId);
    }

    /**
     * 获取用户健康报表id
     * @param userId
     * @return
     * @throws Exception
     */
    private int getReportId(int userId) throws Exception {
        return healthReportDao.getReportId(userId);
    }

    /**
     * 获取用户健康报表预算热量
     * @param userId
     * @return
     * @throws Exception
     */
    private double getCalorieBudget(int userId) throws Exception {
        return healthReportDao.getCalorieBudget(userId);
    }

    /**
     * 获取用户身材类型
     * @param userId
     * @return
     * @throws Exception
     */
    private String getBodyType(int userId) throws Exception {
        return healthReportDao.getBodyType(userId);
    }

    /**
     * 获取用户健康指数
     * @param userId
     * @return
     * @throws Exception
     */
    private int getIndex(int userId) throws Exception {
        return healthReportDao.getIndex(userId);
    }

    /**
     * 获取用户健康评级
     * @param userId
     * @return
     * @throws Exception
     */
    private String getLevel(int userId) throws Exception {
        return healthReportDao.getLevel(userId);
    }

    /**
     * 获取用户bmi
     * @param userId
     * @return
     * @throws Exception
     */
    private double getBmi(int userId) throws Exception {
        return healthReportDao.getBmi(userId);
    }

    /**
     * 获取用户bfr
     * @param userId
     * @return
     * @throws Exception
     */
    double getBfr(int userId) throws Exception {
        return healthReportDao.getBfr(userId);
    }

    /**
     * 更新用户健康表
     * @param report
     */
    public void updateReport(HealthReport report) throws Exception {
        healthReportDao.updateReport(report);
    }

    /**
     *  判断用户是否已经有健康表
     * @param userId
     * @return
     */
    public boolean hasReport(int userId) throws Exception {
        HealthReport healthReport = healthReportDao.getHealthReport(userId);
        return healthReport==null?false:true;
    }
}
